home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 234_01 / util.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-06-16  |  10.6 KB  |  469 lines

  1. /*
  2.   HEADER: CUG     nnn.nn;
  3.   TITLE:     XDIR - Hard Disk Manager
  4.   VERSION:     1.0 for IBM-PC
  5.   DATE:      Apr 03, 1987
  6.   DESCRIPTION:     Hard Disk Manager for IBM PC
  7.   KEYWORDS:     Hard Disk Manager Dump Directory
  8.   SYSTEM:     IBM-PC and Compatiables
  9.   FILENAME:      util.c
  10.   WARNINGS:     None
  11.   CRC:         N/A
  12.   SEE-ALSO:     HDIR.DOC and XDIR.DOC
  13.   AUTHOR:     Mike Blakley 15645 SW 82 Cir Ln #76, Miami, Fl 33193
  14.   COMPILERS:     ECO-C
  15.   REFERENCES:     XDIR.DOC
  16. */
  17.  
  18.  
  19. /*
  20.    utility routines for xdir
  21.    xrename   - rename a file
  22.    xcopy     - copy files
  23.    xdump     - dump a file  in hex and ascii
  24.    dline     - build a dump line in hex and ascii
  25.    upmod     - change file attributes
  26.    getpath   - get path name
  27. */
  28. #include "stdio.h"
  29. #include "dir.h"
  30. #include "xdir.h"
  31. /*
  32.     xrename
  33.     rename a file
  34.  
  35. */
  36. void xrename(old,new)
  37. char *old, *new;
  38. {
  39.     int  i;
  40.     i = rename(old,new);
  41.     if (i != 0)
  42.        {
  43.        writestr("\nCouldn't rename ");
  44.        writestr(old);
  45.        writestr(" to ");
  46.        writestr(new);
  47.        writestr(" - press enter ");
  48.        getch();
  49.        }
  50.  
  51. }
  52.  
  53. /*
  54.     xcopy
  55.     copy a file
  56.  
  57. */
  58. #define BUFFSIZE 128
  59. void xcopy(fromf,tof)
  60. char *fromf, *tof;
  61. {
  62.      int fdi;
  63.      int fdo;
  64.      static int  i;
  65.      static char buffer[BUFFSIZE];
  66.      static int numbytes;
  67.  
  68.      fdo = creat(tof,0);
  69.      if (fdo == EOF)
  70.         {
  71.         writestr("\nCan't open output file ");
  72.         writestr(tof);
  73.         writestr(" - press any key");
  74.         getch();
  75.         return;
  76.         }
  77.  
  78.      fdi = open(fromf,0);
  79.      if (fdi == EOF)
  80.         {
  81.         writestr("\nCan't open input file ");
  82.         writestr(fromf);
  83.         writestr(" - press any key");
  84.         getch();
  85.         unlink(tof);
  86.         return;
  87.         }
  88.  
  89.       while ((numbytes = read(fdi,buffer,BUFFSIZE)) != EOF)
  90.       {
  91.  
  92.  
  93.       if (numbytes < 0)
  94.          {
  95.          writestr("\nRead error occurred - ");
  96.          writestr(fromf);
  97.          writestr("press any key ");
  98.          getch();
  99.          unlink(tof);
  100.          return;
  101.          }
  102.  
  103.        if (numbytes == 0) break;
  104.        i = write(fdo,buffer,numbytes);
  105.        if (i <= 0)
  106.           {
  107.           writestr("\nError writing output file ");
  108.           writestr(tof);
  109.           writestr(" -press any key");
  110.           getch();
  111.           unlink(tof);
  112.           return;
  113.           }
  114.  
  115.        if (numbytes < BUFFSIZE) break;
  116.        }    /* end while */
  117.  
  118.        i = close(fdi);
  119.        if (i == EOF)
  120.           {
  121.           writestr("\nCouldn't close input ");
  122.           writestr(fromf);
  123.           writestr(" press any key ");
  124.           getch();
  125.           }
  126.  
  127.        i = close(fdo);
  128.        if (i == EOF)
  129.           {
  130.           writestr("\nCouldn't close output ");
  131.           writestr(tof);
  132.           writestr(" press any key ");
  133.           getch();
  134.           }
  135.  
  136. }        /* end function */
  137.  
  138. /*
  139.     xdump
  140.     dump a file
  141.  
  142. */
  143. #define DUMPSIZE 256
  144. void xdump(fnam)
  145. char *fnam;
  146. {
  147.      int    fdi;                   /* file descriptor */
  148.      static int  i,j, k, c;
  149.      static char buffer[DUMPSIZE];
  150.      static int numbytes;
  151.      char   pline[79];
  152.      long   offset;                /* byte offset of record displayed */
  153.      static int currpage, lastpage;
  154.      long   foff, lseek();
  155.      char   temp[20];
  156.  
  157.      currpage = 0;
  158.      offset = 0L;
  159.      fdi = open(fnam,0);
  160.      if (fdi == EOF)
  161.         {
  162.         writestr("\nCan't open input file ");
  163.         writestr(fnam);
  164.         writestr(" - press any key");
  165.         getch();
  166.         return;
  167.         }
  168.       foff =  lseek(fdi,0L,2);                /* search end of file */
  169.       foff /= DUMPSIZE;
  170.       lastpage = (int) foff;                  /* determine last page */
  171.  
  172.       while (1)
  173.       {
  174.       foff = (long) currpage;                /* current page */
  175.       foff *= (long) DUMPSIZE;
  176.       offset = lseek(fdi,foff,0);              /* do a seek to position */
  177.  
  178.  
  179.       numbytes = read(fdi,buffer,DUMPSIZE);
  180.  
  181.  
  182.       if (numbytes < 0)
  183.          {
  184.          writestr("\nRead error occurred - ");
  185.          writestr(fnam);
  186.          writestr("press any key ");
  187.          getch();
  188.          return;
  189.          }
  190.  
  191.  
  192.        k = DUMPSIZE - numbytes;     /* indicate eof fill pattern */
  193.        for (j=0;j<k;j++) buffer[numbytes+j] = 0x1a;
  194.  
  195.        clrscr();
  196.        writestr("\n\033[1mDump of file \033[0m");
  197.        writestr("\033[7m");
  198.        writestr(fnam);
  199.        writestr("\033[0m");
  200.        writestr("\n\n\n\n");
  201.  
  202.        for (i=0;i<16;i++)
  203.        {
  204.        j = i * 16;
  205.        dline(buffer+j,pline,offset+(long)j);   /* build dump line */
  206.        putchar('\n');
  207.        writestr(pline);
  208.        }
  209.  
  210.        writestr("\n\n\n\033[7mEnd of page -\033[0m");
  211.        writestr("\033[1mESC\033[0m = Exit PgUp PgDn Home End +n -n F1=Help ");
  212.  
  213.        c = toupper(getch());
  214.        if (c == 0) c= getch();     /* pickup up special keys */
  215.  
  216.        if (c == '\033') break;
  217.        else
  218.        if (c == 73)    /* PgUp */ --currpage;
  219.        else
  220.        if (c == 81)    /* PgDn */ ++currpage;
  221.        else
  222.        if (c == 13)    /* CR   */ ++currpage;
  223.        else
  224.        if (c == 79)    /* End  */ currpage = lastpage;
  225.        else
  226.        if (c == 71)    /* Home */ currpage = 0;
  227.        else
  228.        if (c == '+')
  229.           {
  230.           putchar(c);
  231.           gets(temp);
  232.           currpage += atoi(temp);
  233.           }
  234.        else
  235.        if (c == '-')
  236.           {
  237.           putchar(c);
  238.           gets(temp);
  239.           currpage -= atoi(temp);
  240.           }
  241.        else
  242.        if (c == 59)     /* Help */
  243.           {
  244.           clrscr();
  245.           writestr(" Hex/Ascii Dump - Help Screen ");
  246.           writestr("\n\n\nKey                Function");
  247.           writestr("\n_____________________________\n");
  248.           writestr("\nPgUp               Previous Page");
  249.           writestr("\nPgDn               Next Page    ");
  250.           writestr("\nHome               Restart at beginning ");
  251.           writestr("\nEnd                Go to End    ");
  252.           writestr("\nF1                 Help - This screen ");
  253.           writestr("\n+nnn               Forward  nnn 256 byte blocks ");
  254.           writestr("\n-nnn               Backward nnn 256 byte blocks ");
  255.           writestr("\n<CR>               Next Page ");
  256.           writestr("\n\n\nPress any key to continue ");
  257.           getch();
  258.           }
  259.  
  260.        if (currpage < 1) currpage = 0;
  261.        if (currpage > lastpage) currpage = lastpage;
  262.  
  263.        }    /* end while */
  264.  
  265.        i = close(fdi);
  266.        if (i == EOF)
  267.           {
  268.           writestr("\nCouldn't close input ");
  269.           writestr(fnam);
  270.           writestr(" press any key ");
  271.           getch();
  272.           }
  273.  
  274. }        /* end function */
  275.  
  276.  
  277. /*
  278.    dline
  279.    build a dump line from 16 bytes
  280.  
  281. */
  282. void dline(ibuff,xbuff,offset)
  283. char *ibuff, *xbuff;
  284. long  offset;
  285. {
  286.      int  i,j,k,c;
  287.      char lbuff[80], rbuff[20], obuff[10];
  288.      char *cpl, *cpr;
  289.  
  290.  
  291.      clear(lbuff,80,' ');
  292.      clear(rbuff,20,' ');
  293.      cpl = lbuff;
  294.      cpr = rbuff;
  295.  
  296.      j = k = (int) offset;
  297.      j &= 0xf000;
  298.      j >>= 12;
  299.      j &= 0x0f;
  300.      if (j < 10) obuff[0] = j + '0';
  301.         else     obuff[0] = j + 'a' - 10;
  302.      j = k & 0x0f00;
  303.      j >>= 8;
  304.      if (j < 10) obuff[1] = j + '0';
  305.         else     obuff[1] = j + 'a' - 10;
  306.      j = k & 0x00f0;
  307.      j >>= 4;
  308.      if (j < 10) obuff[2] = j + '0';
  309.         else     obuff[2] = j + 'a' - 10;
  310.      j = k & 0x000f;
  311.      if (j < 10) obuff[3] = j + '0';
  312.         else     obuff[3] = j + 'a' - 10;
  313.      obuff[4] = 0;
  314.  
  315.      for (i=0;i<16;i++)
  316.      {
  317.      c = (int) *ibuff++;   /* get character */
  318.      if ((c >= 0x20) && (c <= 0x7f))
  319.         *cpr++ = c;
  320.         else *cpr++ = '.';
  321.  
  322.      j = (c & 0xf0);
  323.      j >>= 4;
  324.      if (j < 10) *cpl++ = j + '0';
  325.         else *cpl++ = j + 'a' - 10;
  326.  
  327.      j = (c & 0x0f);
  328.      if (j < 10) *cpl++ = j + '0';
  329.         else *cpl++ = j + 'a' - 10;
  330.      *cpl++ = ' ';
  331.      }
  332.      *cpl = *cpr = 0;
  333.  
  334.      strcpy(xbuff,obuff);
  335.      strcat(xbuff,"  ");
  336.      strcat(xbuff,lbuff);
  337.      strcat(xbuff,"    ");
  338.      strcat(xbuff,rbuff);
  339.  
  340. }    /* end of dline */
  341.  
  342.  
  343. /*
  344.   upmod
  345.   modify file attribute byte
  346.  
  347. */
  348. void upmod(fnam,atstr)
  349. char *fnam, *atstr;
  350. {
  351.    int  i,att,c,watt;
  352.    static DIR *dir;
  353.    struct direct *dir_entry;
  354.    char *cp;
  355.    int  flag;
  356.    char pathname[80];
  357.    char workname[80];
  358.    int  path;
  359.    int  unhide;       /* determine if unhide is required */
  360.  
  361.    cp = atstr;        /* decode the string   */
  362.    flag = watt = unhide = 0;   /* initialize settings */
  363.  
  364.    while (c=toupper(*cp++))    /* decode desired attributes */
  365.    {
  366.    switch (c)
  367.    {
  368.    case 'X':   /* reset */
  369.      flag = 1;
  370.      break;
  371.    case 'U':   /* unhide */
  372.      unhide = 1;
  373.      break;
  374.    case 'R':   /* read only */
  375.      watt |= 0x01;
  376.      break;
  377.    case 'H':   /* hidden    */
  378.      watt |= 0x02;
  379.      break;
  380.    case 'S':   /* system    */
  381.      watt |= 0x04;
  382.      break;
  383.    case 'L':   /* label     */
  384.      watt |= 0x08;
  385.      break;
  386.    case 'D':   /* directory */
  387.      watt |= 0x10;
  388.      break;
  389.    case 'A':   /* archive   */
  390.      watt |= 0x20;
  391.      break;
  392.    }          /* end switch */
  393.    }          /* end while */
  394.  
  395.    path = getpath(fnam,pathname);    /* determine if wild cards */
  396.  
  397.    dir = opendir(fnam);
  398.    setdirat(dir,0x3f);     /* search attributes are all */
  399.    while (1)
  400.    {
  401.    dir_entry = readdir(dir);    /* read the directory */
  402.    if (dir_entry == NULL) break;
  403.    writestr("\nProcessing ");
  404.    writestr(dir_entry->d_name);
  405.    att = (int) dir_entry->d_attrib;
  406.    if ((att == 0x08) || (att == 0x10)) continue;
  407.    if (flag)  att = watt;
  408.       else att |= watt;
  409.    if (unhide) att &= 0x3d;      /* reset the "hide" bit */
  410.  
  411.    if (path)
  412.       {
  413.       strcpy(workname,pathname);
  414.       strcat(workname,dir_entry->d_name);
  415.       }
  416.    else strcpy(workname,fnam);
  417.  
  418.    i = chmod(workname,att);
  419.    }
  420.  
  421.    closedir(dir);    /* free up the memory */
  422.  
  423. }
  424.  
  425. /*
  426.   getpath
  427.   determine if a filename contains wild cards
  428.     and if so, extract the path name
  429.  
  430. */
  431. getpath(fnam,pnam)
  432. char *fnam;      /* filename */
  433. char *pnam;      /* pathname */
  434. {
  435.     int  i,j,found,c;
  436.  
  437.     char *cp, *cpi;
  438.     char work[80];
  439.  
  440.  
  441.     strcpy(work,fnam);
  442.  
  443.     cp = work;
  444.     found = 0;
  445.     while (c = (int) *cp++)
  446.     {
  447.     if ((c == '?') || (c=='*')) {found = 1;break;}
  448.     }
  449.  
  450.     if (found)  ;
  451.     else {*pnam = 0;return 0;}
  452.  
  453.     i = strlen(work);
  454.     cpi = work + i;
  455.     --cpi;
  456.  
  457.     for (j=0;j<i;j++)
  458.     {
  459.     if ((c = (int) *cpi--) == '\\') {++cpi;*cpi=0;break;}
  460.     }
  461.  
  462.     strcat(work,"\\");
  463.     strcpy(pnam,work);
  464.     return (1);
  465.  
  466. }
  467.  
  468.  
  469.